﻿using KingerRobot.MexicoSailunMDC.SpiService;

namespace KingerRobot.MexicoSailunMDC.Biz;
/// <summary>
/// 入库取胎位绑定任务
/// </summary>
public class WarehousingInputBiz : K8.MDCBB.Biz
{
    private const string ClassName = nameof(WarehousingInputBiz);
    #region 配置参数
    private ParamData Param { get; set; }

    class ParamData
    {
        /// <summary>
        /// 设备位置ID
        /// </summary>
        public long CrnLocId { get; set; }
  
    }
    #endregion

    #region PLC 通讯参数
    private ValueData Data { get; set; }
    class ValueData
    {
        //任务号
        public int TaskNo { get; set; }
       
    }
    #endregion

    #region 缓存
    private CacheData Cache { get; set; }
    class CacheData
    {
        //任务号
        public int? TaskNo { get; set; }
        //条码
        public string ProductCode { get; set; }

    }
    #endregion

    #region 实时日志
    private StepData Step { get; set; }
    class StepData
    {
        private string _msg = string.Empty;
        public string StepMsg
        {
            get
            {
                return this._msg;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this._msg = string.Empty;
                    return;
                }
                this._msg = $"{ClassName}|{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}>>{value}";
            }
        }
        public int RStatus { get; set; }
    }
    private void StepMsg(string msg)
    {
        this.Step.StepMsg = msg;
        WriteLog(msg);
    }
    #endregion

    #region 文件日志（运行需要 Nlog 配置）
    private void WriteLog(string message)
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = message });
    }
    #endregion 

    /// <summary>
    /// 和 PLC 通讯对象
    /// </summary>
    private BizData BizData { get; set; }
    public int Execute(BizData writer)
    {
        // Excute 方法外使用 BizData
        this.BizData = writer;
        // 返回参数
        var result = 0;
        this.StepMsg($"开始绑定.");
        // 获取条码
        var CrnLocId = Param.CrnLocId;
        var taskNo = Data.TaskNo;

        if (taskNo == 0)
        {
            this.StepMsg($"条码为空.");
            return result;
        }
        var taskService = K8.RSF.Client.Create<ITaskService>();
       var productCode = taskService.GetPoductCodeByTaskNo(taskNo);
        if (String.IsNullOrEmpty(productCode))
        {
            this.StepMsg($"条码为空.");
            return result;
        }
        if (productCode == ConstFields.Default)
        {
            this.StepMsg($"条码为0,{productCode}.");
            return result;
        }

        if (taskNo == Cache.TaskNo)
        {
            this.StepMsg($"{taskNo}任务已绑定位置{CrnLocId}.");
            return result;
        }

        //var taskNo = Value.TaskNo;
        this.StepMsg($"入库库位{CrnLocId}|" +
            $"创建任务参数(TaskNo):{taskNo}, 胎胚条码:{productCode},{DateTime.Now.ToShortDateString()}");

        // 修改货位TaskId
        var positionService = K8.RSF.Client.Create<IPositionService>();
        var num = positionService.UpdateRcsLocationIsProductCode(CrnLocId, productCode, taskNo, true);

        if (num == -5)
        {
            this.StepMsg($"条码{productCode} 已在库中存在");
        }
        else
        {
            this.StepMsg($"条码{productCode}任务{taskNo}绑定返回值：{num}.");
        }

        if (num > 0)
        {
            Cache.TaskNo = taskNo;
        }
        return result;

    }

}
